<article id="wikiArticle">
<div></div>
<h2 id="概述">概述</h2>
<p><code><strong>anchor()</strong></code> 方法创建一个 <a href="/zh-CN/docs/Web/HTML/Element/a" title="HTML &lt;a&gt; 元素（或称锚元素）可以创建通向其他网页、文件、同一页面内的位置、电子邮件地址或任何其他 URL 的超链接。"><code>&lt;a&gt;</code></a> HTML 锚元素，被用作超文本靶标（hypertext target）。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code>str.anchor(<em>name</em>) </code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>name</code></dt>
<dd>一个字符串，表示被创建的标签的 <code>name</code> 属性。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>使用 <code>anchor</code> 方法能够以编程方式在一个文档中创建和展现一个锚链接。</p>
<p>语法上来讲，字符串表示你想让用户看到的文本。<code>name</code> 字符串参数表示 <a href="/zh-CN/docs/Web/HTML/Element/a" title="HTML &lt;a&gt; 元素（或称锚元素）可以创建通向其他网页、文件、同一页面内的位置、电子邮件地址或任何其他 URL 的超链接。"><code>&lt;a&gt;</code></a> 元素的 <code>name</code> 属性。</p>
<p>使用 anchor 方法创建的锚点（anchors）将会成为 <a href="/zh-CN/docs/Web/API/Document/anchors" title="anchors属性返回当前文档中的所有锚点元素."><code>document.anchors</code></a> 数组的元素。</p>
<h2 id="示例">示例</h2>
<h3 id="例子：使用_anchor">例子：使用 <code>anchor</code></h3>
<pre><code class="language-js">var myString = "Table of Contents";

document.body.innerHTML = myString.anchor("contents_anchor");</code></pre>
<p>将会输出下面的 HTML：</p>
<pre><code class="language-html">&lt;a name="contents_anchor"&gt;Table of Contents&lt;/a&gt;</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">注释</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.anchor" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.anchor</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition. Implemented in JavaScript 1.0.<br/>
    Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>1.0 (1.7 or earlier)</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>1.0 (1.0)</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h3 id="Gecko-specific_notes">Gecko-specific notes</h3>
<p>从<span title="(Firefox 17 / Thunderbird 17 / SeaMonkey 2.14)">Gecko 17</span>开始  <strong>"</strong> (引号) 被 HTML引用字符 <code>&amp;quot所替代</code>；在字符串中申请<code>命名参数</code>.</p>
<h2 id="See_Also" name="See_Also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/String/link" title="link() 方法创建一个 &lt;a&gt; HTML 元素，用该字符串作为超链接的显示文本，参数作为指向另一个 URL 的超链接。"><code>String.prototype.link()</code></a></li>
</ul>
</article>